package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListVO;
import cn.tedu.csmall.passport.security.AdminDetails;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/admins")
@Api(tags = "1. 管理员模块")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    public AdminController() {
        log.debug("创建控制器对象：AdminController");
    }

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult<Void> login(AdminLoginDTO adminLoginDTO){
        log.debug("开始处理【管理员登陆】的请求，参数：{}",adminLoginDTO);
        adminService.login(adminLoginDTO);
        return JsonResult.ok();
    }

    @PostMapping("/add_new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理【添加管理员】的请求，参数：{}",adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAnyAuthority('/ams/admin/read')")
    @GetMapping("")
    public JsonResult<List<AdminListVO>> list(@AuthenticationPrincipal AdminDetails adminDetails) {
        log.debug("开始处理【查询相册列表】的请求，参数：无");
        log.debug("当事人：{}",adminDetails);
        log.debug("当事人的ID：{}",adminDetails.getId());
        log.debug("当事人的用户名：{}",adminDetails.getUsername());
        List<AdminListVO> list = adminService.list();
        return JsonResult.ok(list);
    }
}
